package com.example.lenovo.downloaddemo.activity;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lenovo.downloaddemo.R;
import com.example.lenovo.downloaddemo.bean.Customer;
import com.example.lenovo.downloaddemo.bean.ReturnBean;
import com.example.lenovo.downloaddemo.http.datasource.MDataSource;
import com.example.lenovo.downloaddemo.http.okhttp.utils.AppContext;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CodeUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.Collintion;
import com.example.lenovo.downloaddemo.http.okhttp.utils.CommonUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.GsonUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.MD5;
import com.example.lenovo.downloaddemo.http.okhttp.utils.SharedPreUtils;
import com.example.lenovo.downloaddemo.http.okhttp.utils.ToastUtil;
import com.example.lenovo.downloaddemo.myview.MyEditText;
import com.example.lenovo.downloaddemo.sqlite.LoadListCallBack;
import com.example.lenovo.downloaddemo.util.GotoActivity;
import com.example.lenovo.downloaddemo.util.PhoneUtil;
import com.example.lenovo.downloaddemo.util.StatusBarUtil;
import com.example.lenovo.downloaddemo.util.TimeUtil;
import com.example.lenovo.downloaddemo.view.TitleBarView;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

public class FindPassWordActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.find_titlebar)
    TitleBarView titleBarView;//标题栏
    @Bind(R.id.find_phone_editText)
    MyEditText find_phone_editText;//手机号
    @Bind(R.id.new_password_editText)
    MyEditText new_password_editText;//密码
    @Bind(R.id.new_password_again_editText)
    MyEditText new_password_again_editText;//确认密码
    @Bind(R.id.findimage_code)
    ImageView findimage_code;//承载验证码的图片
    @Bind(R.id.find_message_code)
    MyEditText find_message_code;//验证码
    @Bind(R.id._comfirm_button)
    TextView _comfirm_button;//确认

    private String imageCode;//验证码

    private String allString;



    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                String returnMessage= (String) msg.obj;
                Gson gson=new Gson();
                try {
                    ReturnBean bean = gson.fromJson(returnMessage, ReturnBean.class);
                    if (bean.getCode() == 200) {
                        ToastUtil.makeText(AppContext.getInstance(), bean.getMsg());
//                    GotoActivity.gotoActiviy(FindPassWordActivity.this,LoginActivity.class);
                        finish();
                        SharedPreUtils.clearDatas(AppContext.getInstance());
                    } else {
                        ToastUtil.makeText(AppContext.getInstance(), bean.getMsg());
                    }
                }catch (Exception e){
                    ToastUtil.makeText(AppContext.getInstance(),e.getMessage());
                }
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_pass_word);
        ButterKnife.bind(this);
        initView();
    }

    private void getChangeInfo(){
        String phone = find_phone_editText.getText().toString().trim();//手机号
        String password=new_password_editText.getText().toString().trim();//密码
        String new_Password=new_password_again_editText.getText().toString().trim();//确认密码
        String code=find_message_code.getText().toString().trim();
        if(!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(password)&&!TextUtils.isEmpty(new_Password)&&!TextUtils.isEmpty(code)){
            if(PhoneUtil.isTel(phone)){
                if(password.equals(new_Password)){
                    if(code.equals(imageCode)){
                        changeUserInfo(phone,password,code);
                    }else{
                        ToastUtil.makeText(AppContext.getInstance(),"验证码不正确");
                        findimage_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                        imageCode=CodeUtils.getInstance().getCode().toLowerCase();
                    }
                }else{
                    ToastUtil.makeText(AppContext.getInstance(),"两次输入的密码不同");
                    findimage_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                    imageCode=CodeUtils.getInstance().getCode().toLowerCase();
                }
            }else{
                ToastUtil.makeText(AppContext.getInstance(),"手机号格式不正确");
                findimage_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                imageCode=CodeUtils.getInstance().getCode().toLowerCase();
            }
        }else{
            ToastUtil.makeText(AppContext.getInstance(),"输入不能为空");
            findimage_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
            imageCode=CodeUtils.getInstance().getCode().toLowerCase();
        }

    }

    private void changeUserInfo(String phone,String password,String code){
        HashMap<String, String> map = new HashMap<>();
        map.put("model", "Login");
        map.put("method", "findPassword");
        map.put("user_phone", phone);
        map.put("password", password);
        map.put("code", code);
        map.put("time",new TimeUtil().getMoment());
        Collintion collintion=new Collintion();

        allString=collintion.Sort(map)+"ad1af567dc7bfedd5e760fc1f48a59ae";
        String sign= MD5.GetMD5Code(MD5.GetMD5Code(allString));
        Log.e("拼接字符串",allString);
        Log.e("签名===",sign);
        map.put("sign",sign);
        MDataSource<Customer> dataSource = new MDataSource<>(AppContext.getInstance());
        dataSource.postData(map, new LoadListCallBack() {
            @Override
            public void loadList(List list) {

            }

            @Override
            public void systemError(Request request, String errorInfo, Exception e) {
                Log.e("error", "systemError: " + errorInfo);
                e.printStackTrace();
            }

            @Override
            public void retLoad(String code) {

            }

            @Override
            public void loadString(String string) {
                Log.e("找回密码", "loadString 获取结果 : " + string);

                Message message=Message.obtain();
                message.what=1;
                message.obj=string;
                mHandler.sendMessage(message);





            }
        });
    }
//    @Bind(R.id.find_phone_editText)
//    MyEditText find_phone_editText;//手机号
//    @Bind(R.id.new_password_editText)
//    MyEditText new_password_editText;//密码
//    @Bind(R.id.new_password_again_editText)
//    MyEditText new_password_again_editText;//确认密码
//    @Bind(R.id.findimage_code)
//    ImageView findimage_code;//承载验证码的图片
//    @Bind(R.id.find_message_code)
//    MyEditText find_message_code;//验证码
//    @Bind(R.id._comfirm_button)
//    TextView _comfirm_button;//确认
//
//    private String imageCode;//验证码

    @Override
    protected void initView() {
        StatusBarUtil.setMargin(AppContext.getInstance(),titleBarView);
        titleBarView.setTitle(AppContext.getInstance()
                .getResources().getString(R.string.find_password));
        titleBarView.showBackButton(true);
        findimage_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
        imageCode=CodeUtils.getInstance().getCode().toLowerCase();

        find_phone_editText.setHint(AppContext.getInstance().getResources().getText(R.string.tel_ch));
        new_password_editText.setHint(AppContext.getInstance().getResources().getText(R.string.regist_inputPhone_cn));
        new_password_again_editText.setHint(AppContext.getInstance().getResources().getText(R.string.regist_inputPhone_again_cn));
        find_message_code.setHint(AppContext.getInstance().getResources().getText(R.string.regist_inputMessageCode_cn));
        _comfirm_button.setText(AppContext.getInstance().getResources().getText(R.string.commit));
        initListener();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

    @Override
    protected void initListener() {
        findimage_code.setOnClickListener(this);
        _comfirm_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id._comfirm_button:
                if(CommonUtils.isNetOk(AppContext.getInstance())) {
                    getChangeInfo();
                }else {
                    ToastUtil.makeText(AppContext.getInstance(),"Fail!");
                }
                break;
            case R.id.findimage_code:
                findimage_code.setImageBitmap(CodeUtils.getInstance().createBitmap());
                imageCode=CodeUtils.getInstance().getCode().toLowerCase();
                break;
        }

    }
}
